/**

 Copyright 2001-2011, Motio, Inc.
 All rights reserved.

 **/
package net.emsquared.gamedev.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.tools.imagepacker.TexturePacker;
import net.emsquared.gamedev.TestGame;
import net.emsquared.gamedev.entity.AbstractMoveableEntity;
import net.emsquared.gamedev.entity.Entity;
import net.emsquared.gamedev.entity.collectible.Chest;
import net.emsquared.gamedev.entity.playable.Player;
import net.emsquared.gamedev.maps.MapCoordinates;
import net.emsquared.gamedev.maps.WorldMap;

import java.util.ArrayList;
import java.util.List;

public class MainScreen implements IScreen
{
   private TestGame controller;
   private Player player;
   private OrthographicCamera camera;
   private WorldMap map;

   private BitmapFont chestCountLabel;

   private SpriteBatch spriteBatch;

   private List<Chest> chests = new ArrayList<Chest>(6);

   ControlBar controlBar;

   @Override
   public void initialize(TestGame aController)
   {
      controller = aController;
      chestCountLabel = new BitmapFont();
      chestCountLabel.setColor(Color.WHITE);

      map = new WorldMap(Gdx.files.internal("data/country/country.tmx"), Gdx.files.internal("data/country"));
      player = new Player("images/walker.png", this, 1, 1);
      initializeChests();

      spriteBatch = new SpriteBatch();

      camera = new OrthographicCamera(TestGame.GAME_WIDTH, TestGame.GAME_HEIGHT);
      centerCameraOnPlayer();

//      controlBar = new ControlBar(this, camera);
   }

   public void initializeChests()
   {
      int chestCount = 0;
      while(chestCount < 1)
      {
         //generate random chests somewhere on the map.
         int randomX = (int)(Math.random() * (map.getMapWidth_Tiles() - 1));
         int randomY = (int)(Math.random() * (map.getMapHeight_Tiles() - 1));

         //test to see if it collides with any of the existing chests
         Rectangle possibleChest = new Rectangle(randomX, randomY, 32f, 32f);
         boolean shouldAdd = true;
         for(Chest existingChest : chests)
         {
            if(possibleChest.overlaps(existingChest.getBoundingRectangle()))
            {
               shouldAdd = false;
            }
         }
         if(shouldAdd)
         {
            chests.add(new Chest("images/chest.png", this, randomX, randomY));
            chestCount++;
         }
      }
   }

   @Override
   public void start()
   {
      //To change body of implemented methods use File | Settings | File Templates.
   }

   @Override
   public void update()
   {
      // Handle input from the user
      handleInput();
      // Pass delta time to user to move if applicable
      player.move(Gdx.graphics.getDeltaTime());
      // Check to see if there are any collisions
      checkChestCollisions();
      // Adjust camera to be centered on camera
      centerCameraOnPlayer();
   }

   public void handleInput()
   {
      if(Gdx.input.isKeyPressed(Input.Keys.LEFT))
      {
         player.moveLeft();
      }
      if(Gdx.input.isKeyPressed(Input.Keys.RIGHT))
      {
         player.moveRight();
      }
      if(Gdx.input.isKeyPressed(Input.Keys.UP))
      {
         player.moveUp();
      }
      if(Gdx.input.isKeyPressed(Input.Keys.DOWN))
      {
         player.moveDown();
      }
   }

   public void checkChestCollisions()
   {
//      List<Chest> openedChests = new ArrayList<Chest>(1);
//      for(Chest chest : chests)
//      {
//         if(player.collidesWith(chest))
//         {
//            player.openChest(chest);
//            openedChests.add(chest);
//         }
//      }
//
//      chests.removeAll(openedChests);
   }

   @Override
   public void draw()
   {
      Gdx.graphics.getGL10().glClear(GL10.GL_COLOR_BUFFER_BIT);
      camera.update();
      spriteBatch.setProjectionMatrix(camera.combined);

      map.renderMap(camera);

      spriteBatch.begin();
      player.draw(spriteBatch);
      for(Chest chest : chests)
      {
         chest.draw(spriteBatch);
      }
      chestCountLabel.draw(spriteBatch, "Chest count: " + player.getChestCount(), camera.position.x - (camera.viewportWidth/2 - 16), camera.position.y - (camera.viewportHeight/2 - 16));
//      controlBar.draw(spriteBatch, camera);
      spriteBatch.end();
   }

   @Override
   public void resize(int aWidth, int aHeight)
   {
      //To change body of implemented methods use File | Settings | File Templates.
   }

   @Override
   public void pause()
   {
      //To change body of implemented methods use File | Settings | File Templates.
   }

   @Override
   public void dispose()
   {
      //To change body of implemented methods use File | Settings | File Templates.
   }

   public void centerCameraOnPlayer()
   {
      float x;
      float y;

      if (player.getXPos_Pix() > map.getMapWidth_Pix() - (TestGame.GAME_WIDTH / 2))
      {
         x = map.getMapWidth_Pix() - (TestGame.GAME_WIDTH / 2);
      }
      else if (player.getXPos_Pix() < (TestGame.GAME_WIDTH / 2))
      {
         x = TestGame.GAME_WIDTH / 2;
      }
      else
      {
         x = player.getXPos_Pix();
      }

      if (player.getYPos_Pix() > map.getMapHeight_Pix() - (TestGame.GAME_HEIGHT / 2))
      {
         y = map.getMapHeight_Pix() - (TestGame.GAME_HEIGHT / 2);
      }
      else if (player.getYPos_Pix() < (TestGame.GAME_HEIGHT / 2))
      {
         y = TestGame.GAME_HEIGHT / 2;
      }
      else
      {
         y = player.getYPos_Pix();
      }

      camera.position.set(x, y, 0);
   }

   public boolean canMoveTo(MapCoordinates aMapCoordinates)
   {
      for(Chest eachChest : chests)
      {
         if(eachChest.getMapCoordinates().equals(aMapCoordinates))
         {
            return false;
         }
      }

      return true;
   }
}
